<template>
  <el-cascader :value="value" :options="depts" :props="props" separator="-" size="mini" @change="handleChange" />
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { translateListToTree } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      depts: [],
      props: {
        // 自定义属性字段对象
        value: 'id', // 展示的字段
        label: 'name' // 存储的字段
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartmentList()
      // console.log(res)
      this.depts = translateListToTree(res, 0)
      console.log(this.depts)
    },
    // 级联组件选中项切换
    handleChange(list) {
      if (list.length) {
        // 选中的部门id
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
